package entity

import (
	"time"

	"gorm.io/gorm"
)

// User 用户实体
type User struct {
	ID        uint           `json:"id" gorm:"primaryKey;column:id"`
	Username  string         `json:"username" gorm:"size:32;uniqueIndex;not null;column:username"`
	Password  string         `json:"-" gorm:"size:128;not null;column:password"` // 密码不返回给前端
	Email     string         `json:"email" gorm:"size:128;uniqueIndex;not null;column:email"`
	Phone     string         `json:"phone" gorm:"size:20;column:phone"`
	Role      string         `gorm:"size:20;default:'user';column:role" json:"role"`
	Status    int            `json:"status" gorm:"default:1;column:status"` // 1: 正常, 0: 禁用
	CreatedAt time.Time      `json:"created_at" gorm:"column:created_at"`
	UpdatedAt time.Time      `json:"updated_at" gorm:"column:updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index;column:deleted_at" json:"-"`
}

// TableName 指定表名
func (User) TableName() string {
	return "users"
}

// NewUser 创建新用户
func NewUser(username, password, email string) *User {
	return &User{
		Username: username,
		Password: password,
		Email:    email,
		Status:   1,
	}
}

// IsAdmin 检查用户是否为管理员
func (u *User) IsAdmin() bool {
	return u.Role == "admin"
}

// IsActive 检查用户是否处于活动状态
func (u *User) IsActive() bool {
	return u.Status == 1
}
